package cn.tedu.jxc.service.impl;

import cn.tedu.jxc.ex.ServiceException;
import cn.tedu.jxc.mapper.RoleMapper;
import cn.tedu.jxc.pojo.dto.RoleDTO;
import cn.tedu.jxc.pojo.dto.RoleUpdateDTO;
import cn.tedu.jxc.pojo.entity.Role;
import cn.tedu.jxc.pojo.vo.RoleSelectVO;
import cn.tedu.jxc.service.RoleService;
import cn.tedu.jxc.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    /**
     * 根据id删除角色
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        if (id == 1) {
            String message = "删除角色失败，尝试删除的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        RoleSelectVO roleSelectVO = roleMapper.selectById(id);
        if (roleSelectVO == null) {
            String message="删除失败,角色不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int row = roleMapper.deleteById(id);
        if (row!=1){
            String message ="删除失败,服务器忙,稍后再试!";
            log.debug(message);
            throw  new ServiceException(ServiceCode.ERR_CONFLICT,message );
        }
        log.debug("删除成功!");
    }

    /**
     * 添加角色
     * @param roleDTO
     */
    @Override
    public void addNew(RoleDTO roleDTO) {
        int count = roleMapper.countByName(roleDTO.getName());
        if (count>0){
            String message="添加失败,角色名已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO,role);
        int row = roleMapper.insert(role);
        if (row!=1){
            String message ="添加失败,服务器忙,稍后再试!";
            log.debug(message);
            throw  new ServiceException(ServiceCode.ERR_CONFLICT,message );
        }
        log.debug("添加成功!");
    }

    /**
     * 获取要修改的角色信息
     * @param id
     * @return
     */
    @Override
    public RoleSelectVO getUpdateInfo(Integer id) {
        RoleSelectVO roleSelectVO = roleMapper.selectById(id);
        if (roleSelectVO == null) {
            String message = "删除失败,角色名不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("查询角色信息成功!");
        return roleSelectVO;
    }

    /**
     * 修改角色信息
     * @param roleUpdateDTO
     */
    @Override
    public void update(RoleUpdateDTO roleUpdateDTO) {

        RoleSelectVO roleSelectVO = roleMapper.selectById(roleUpdateDTO.getId());
        if (roleSelectVO == null) {
            String message = "修改失败,角色名存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Integer roleId= roleUpdateDTO.getId();

        RoleSelectVO roleSelectVO1 = roleMapper.getByName(roleUpdateDTO.getName());
        if (roleSelectVO1!=null && !roleId.equals(roleSelectVO1.getId())){
            String message = "修改失败,角色名已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleUpdateDTO, role);
        int row = roleMapper.update(role);
        if (row != 1) {
            String message = "修改失败,服务器忙,稍后再试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        log.debug("修改成功!");
    }

    @Override
    public List<String> getRoleName() {
        List<String> roleName = roleMapper.getRoleName();
        log.debug("获取的角色名有{}",roleName);
        return roleName;
    }

    @Override
    public Integer[] getCountOrderByRole() {
        Integer[] count = roleMapper.getCountOrderByRole();
        log.debug("获取的人数有有{}", Arrays.toString(count));
        return count;
    }
}
